﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;

namespace GUI
{
    public partial class frmXemThucDon : Form
    {
        public frmXemThucDon()
        {
            InitializeComponent();
        }

        private void frmXemThucDon_Load(object sender, EventArgs e)
        {
            layTuan();
            
            khoitaocbbNgay();
            
            
        }
        private void khoitaocbbNgay()
        {
            cbbNgay.Items.Clear();
            for (int i = 2; i < 8; i++)
            {
                cbbNgay.Items.Add("Thứ "+i);
            }
            cbbNgay.Items.Add("Chủ Nhật");
            cbbNgay.SelectedIndex = 0;
            HienThiNgay();
        }
        private void HienThiNgay()
        {
            try
            {
                if (gvDSNgay.RowCount < 1)
                {
                    return;
                }
                for (int i = 0; i < gvDSNgay.RowCount; i++)
                {
                    cbbNgay.Items.Remove(gvDSNgay.Rows[i].Cells[3].Value.ToString());
                }

                cbbNgay.SelectedIndex = 0;
            }
            catch (Exception) { }
            
        }
        List<ThucDonPublic> week = new List<ThucDonPublic>();
        public void layTuan()
        {
            LapThucDonBus busTD = new LapThucDonBus();

            week = busTD.getWeek();
            
            cbbTuan.DataSource = week;
            cbbTuan.DisplayMember = "Tuan";
            cbbTuan.ValueMember = "Tuan";
        }

      
        List<ThucDonPublic> arrdate = new List<ThucDonPublic>();
        private void cbbTuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            chon();
        }

        private void chon()
        {
            LapThucDonBus busTD = new LapThucDonBus();
            string tuan = cbbTuan.SelectedValue.ToString().Trim();
            arrdate = busTD.NgayTrongTuan(tuan);
            gvDSNgay.DataSource = arrdate;
            if (gvDSNgay.Rows.Count > 0)
            {
                int ma = int.Parse(gvDSNgay.Rows[0].Cells["clMaThucDon"].Value.ToString());
                DanhSachThucDon(ma);
                btnNgay.Enabled = true;
                btnXoaNgay.Enabled = true;
            }
            
            if (gvDSNgay.Rows.Count >= 7)
            {
                btnNgay.Enabled = false;
            }
            khoitaocbbNgay();
        }
        private void gvDSNgay_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {

                hienthi();
            }
        }


        public void DanhSachThucDon(int mathucdon)
        {
            gvDSMonAn.Rows.Clear();
            LapThucDonBus busTD = new LapThucDonBus();

            List<CHITIET_THUCDON> arrChiTiet = busTD.DanhSachThucDon(mathucdon);
            for (int i = 0; i < arrChiTiet.Count; i++)
            {
                DataGridViewRow _newRow = (DataGridViewRow)gvDSMonAn.Rows[0].Clone();
                _newRow.Cells[0].Value = arrChiTiet[i].MaThucDon;
                _newRow.Cells[1].Value = arrChiTiet[i].MaMonAn;
                _newRow.Cells[2].Value = arrChiTiet[i].MONAN.TenMonAn;
                _newRow.Cells[3].Value = arrChiTiet[i].SoPhanAn;
                gvDSMonAn.Rows.Add(_newRow);

            }
        }
        private void gvDSNgay_KeyPress(object sender, KeyPressEventArgs e)
        {
            hienthi();
        }
        public void hienthi()
        {
            try
            {
                if (gvDSNgay.Rows.Count < 1)
                    return;
                int ma = int.Parse(gvDSNgay.CurrentRow.Cells["clMaThucDon"].Value.ToString());
                DanhSachThucDon(ma);
            }
            catch (Exception) { }
        }

        private void gvDSNgay_KeyDown(object sender, KeyEventArgs e)
        {
            hienthi();
        }

        private void gvDSNgay_KeyUp(object sender, KeyEventArgs e)
        {
            hienthi();
        }

        private void btnThemNgay_Click(object sender, EventArgs e)
        {
            ThemThucDon();
            khoitaocbbNgay();
            pnNgayTrongTuan.Visible = false;
        }
        public void ThemThucDon()
        {
            THUCDON_NGAY ThucDon = new THUCDON_NGAY();
            LapThucDonBus busTD = new LapThucDonBus();
           
            ThucDon.Ngay = Convert.ToDateTime(gvDSNgay.Rows[0].Cells[2].Value.ToString());
            ThucDon.NgayTrongTuan = cbbNgay.Text;
            ThucDon.Tuan = gvDSNgay.Rows[0].Cells[1].Value.ToString();
            bool result = busTD.ThemThucDon(ThucDon);

            if (result)
            {
                chon();
               // string mathucdon = ThucDon.MaThucDon.ToString();
                List<string> td = new List<string>();
                td.Add(ThucDon.MaThucDon.ToString());
                td.Add(ThucDon.Tuan.ToString());
                td.Add(ThucDon.NgayTrongTuan);
                td.Add("i");
                frmLapThucDon frm = new frmLapThucDon(td);
                frm.ShowDialog();
                chon();
            }    
        }

        private void btnNgay_Click(object sender, EventArgs e)
        {
            pnNgayTrongTuan.Visible = true;
            //HienThiNgay();
            khoitaocbbNgay();
        }

        private void btnHuyNgay_Click(object sender, EventArgs e)
        {
            pnNgayTrongTuan.Visible = false;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            THUCDON_NGAY ThucDon = new THUCDON_NGAY();
            ThucDon.MaThucDon = int.Parse(gvDSNgay.CurrentRow.Cells[0].Value.ToString());
            ThucDon.Ngay = Convert.ToDateTime(gvDSNgay.CurrentRow.Cells[2].Value.ToString());
            ThucDon.NgayTrongTuan = gvDSNgay.CurrentRow.Cells[3].Value.ToString();
            ThucDon.Tuan = gvDSNgay.CurrentRow.Cells[1].Value.ToString();

            List<string> td = new List<string>();
            td.Add(ThucDon.MaThucDon.ToString());
            td.Add(ThucDon.Tuan.ToString());
            td.Add(ThucDon.NgayTrongTuan);
            td.Add("u");
            frmLapThucDon frm = new frmLapThucDon(td);
            frm.ShowDialog();
            chon();
        }

        private void btnXoaNgay_Click(object sender, EventArgs e)
        {
           
            DialogResult dialogResult = MessageBox.Show("Bạn Thật Sự Muốn Xoá Ngày?, Danh Sách Các Món Ăn Trong Ngày Sẻ Bị Xoá?", "Cảnh Báo!", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                LapThucDonBus busTD = new LapThucDonBus();
                THUCDON_NGAY ThucDon = new THUCDON_NGAY();
                ThucDon.MaThucDon = int.Parse(gvDSNgay.CurrentRow.Cells[0].Value.ToString());
                busTD.XoaChiTiet(ThucDon.MaThucDon);

                Boolean temp = busTD.XoaThucDon(ThucDon);
                if (temp)
                {
                    MessageBox.Show("Xoá Thành Công");
                    chon();
                    //HienThiNgay();
                    khoitaocbbNgay();
                }
            }
            else if (dialogResult == DialogResult.No)
            {
                return;
            }
        }

    }
}
